package com.kuhyygy.uitl;

import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import com.kuhyygy.bean.CommonUI;

/**
 * 内存存储器
 * 
 * @author acer
 *
 */
public final class MemoryLoader {
	
	private static MemoryLoader instance;
	private static List<CommonUI> commonUI;
	
	public static MemoryLoader getInstance() {
		if (null == instance)
			return new MemoryLoader();
		return instance;
	}

	public List<CommonUI> loadCommonUI(XmlPullParser pullPar) {
		if (commonUI == null) {
			commonUI = new ArrayList<CommonUI>();
			try {
				int eventType = pullPar.getEventType();
				CommonUI p = null;
				while (eventType != XmlPullParser.END_DOCUMENT) {
					switch (eventType) {
					case XmlPullParser.START_DOCUMENT:
						break;
					case XmlPullParser.START_TAG: {
						if ("menuItem".equals(pullPar.getName())) {
							p = new CommonUI();
						} else if (null != p) {
							if ("name".equals(pullPar.getName())) {
								p.setName(pullPar.nextText());
							} else if ("linkActivity".equals(pullPar.getName())) {
								p.setToPage(pullPar.nextText());
							}
						}
						break;
					}
					case XmlPullParser.END_TAG: {
						if ("menuItem".equals(pullPar.getName())) {
							if (null != commonUI && null != p) {
								commonUI.add(p);
								p = null;
							}
						}
						break;
					}
					default:
						break;
					}
					eventType = pullPar.next();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return commonUI;
	}
}
